<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${BASE_PACKAGE}.dao.mapper.${tableInfo.className?cap_first}Mapper">

    <sql id="Base_Column_List">
        <#list tableInfo.columns as column>${column.name}<#if column_has_next>, </#if></#list>
    </sql>

    <insert id="add" parameterType="${BASE_PACKAGE}.model.dto.${tableInfo.className?cap_first}DTO">
        INSERT INTO ${tableInfo.tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#list tableInfo.columns as column>
            <if test="${column.javaName} != null">${column.name},</if>
            </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <#list tableInfo.columns as column>
            <if test="${column.javaName} != null">${r"#{"}${column.javaName}${r"}"},</if>
            </#list>
        </trim>
    </insert>

    <update id="modify" parameterType="${BASE_PACKAGE}.model.dto.${tableInfo.className?cap_first}DTO">
        UPDATE ${tableInfo.tableName}
        <set>
            <#list tableInfo.columns as column>
            <if test="${column.javaName} != null">${column.name} = ${r"#{"}${column.javaName}${r"}"},</if>
            </#list>
        </set>
        WHERE id = ${r"#{id}"}
    </update>

    <delete id="remove">
        DELETE FROM ${tableInfo.tableName} WHERE id = ${r"#{id}"}
    </delete>

    <select id="page" resultType="${BASE_PACKAGE}.model.dto.${tableInfo.className?cap_first}DTO">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${tableInfo.tableName } AS ${tableInfo.tableName?replace("`", "")?split("_")?map(x -> x?substring(0, 1)?lower_case)?join("")}
        <where>
            <if test="param != null">
                <#list tableInfo.columns as column>
                <#if column.javaType == "String">
                    <if test="param.${column.javaName} != null and param.${column.javaName} != ''">
                        AND ${tableInfo.tableName?replace("`", "")?split("_")?map(x -> x?substring(0, 1)?lower_case)?join("")}.${column.name} = ${r"#{param."}${column.javaName}${r"}"}
                    </if>
                <#else>
                    <if test="param.${column.javaName} != null">
                        AND ${tableInfo.tableName?replace("`", "")?split("_")?map(x -> x?substring(0, 1)?lower_case)?join("")}.${column.name} = ${r"#{param."}${column.javaName}${r"}"}
                    </if>
                </#if>
                </#list>
            </if>
        </where>
    </select>

    <select id="list" resultType="${BASE_PACKAGE}.model.dto.${tableInfo.className?cap_first}DTO">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${tableInfo.tableName } AS ${tableInfo.tableName?replace("`", "")?split("_")?map(x -> x?substring(0, 1)?lower_case)?join("")}
        <where>
            <if test="param != null">
                <#list tableInfo.columns as column>
                <#if column.javaType == "String">
                    <if test="param.${column.javaName} != null and param.${column.javaName} != ''">
                        AND ${tableInfo.tableName?replace("`", "")?split("_")?map(x -> x?substring(0, 1)?lower_case)?join("")}.${column.name} = ${r"#{param."}${column.javaName}${r"}"}
                    </if>
                <#else>
                    <if test="param.${column.javaName} != null">
                        AND ${tableInfo.tableName?replace("`", "")?split("_")?map(x -> x?substring(0, 1)?lower_case)?join("")}.${column.name} = ${r"#{param."}${column.javaName}${r"}"}
                    </if>
                </#if>
                </#list>
            </if>
        </where>
    </select>

    <select id="checkRepeat" resultType="java.lang.Integer">
        SELECT 1
        FROM ${tableInfo.tableName } AS ${tableInfo.tableName?replace("`", "")?split("_")?map(x -> x?substring(0, 1)?lower_case)?join("")}
        WHERE
        delete_flag = '0'
        AND id = #{id}
        <if test="id != null ">
            AND id != #{id}
        </if>
        LIMIT 1
    </select>

</mapper>